package com.gdzl_hadoop.controller;

import com.gdzl_hadoop.common.ConnectToLinux;
import com.gdzl_hadoop.common.ServerResponse;
import com.gdzl_hadoop.pojo.ForestRandom;
import com.gdzl_hadoop.service.IDataHandleService;
import com.gdzl_hadoop.util.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by 1032019725 on 2017/10/18.
 */
@Controller
@RequestMapping("/data/")
public class GetDataController {

    @Autowired
    private IDataHandleService iDataHandleService;

    //region  FPG
    @ResponseBody
    @RequestMapping(value = "checkFPG.do")
    public ServerResponse checkFpgData(String guid) {
/*       // WinToLinux winToLinux = new WinToLinux("118.89.54.21","hadoop","pass@word1");
        ConnectToLinux ct = new ConnectToLinux("118.89.54.21","hadoop","pass@word1");
        InputStream is =  ct.execute("cat result.txt");
      //  String command = "cat "+ path;
      //  InputStream is =  winToLinux.executeCommand(command);
        // String str ="Key: TRSJD:5.45: Value: ([DBYSLTD:1, DXPD:G1, GGBZL:1, GGSY:1, PSTJ:1, YXTCHD:100, YZHCD:1, ZACJDBSD:90, TMPD:G1, PMGX:A1, BCTRZD:SR, TRSJD:5.45],26)";
        // "Key: TRSJD:6.4: Value: ([DBYSLTD:1, DXPD:G1, GGBZL:1, GGSY:1, PSTJ:1, YXTCHD:100, YZHCD:1, ZACJDBSD:90, TRSJD:6.4],31), ([DBYSLTD:1, DXPD:G1, GGBZL:1, GGSY:1, PSTJ:1, YXTCHD:100, YZHCD:1, ZACJDBSD:90, BCTRZD:MR, PMGX:B3, TRSJD:6.4],30), ([DBYSLTD:1, DXPD:G1, GGBZL:1, GGSY:1, PSTJ:1, YXTCHD:100, YZHCD:1, ZACJDBSD:90, TMPD:G1, TRSJD:6.4],30), ([DBYSLTD:1, DXPD:G1, GGBZL:1, GGSY:1, PSTJ:1, YXTCHD:100, YZHCD:1, ZACJDBSD:90, TMPD:G1, BCTRZD:MR, PMGX:B3, TRSJD:6.4],29)";
        //"Key: PMGX:C4: Value: ([DBYSLTD:1, DXPD:G1, GGBZL:1, GGSY:1, PSTJ:1, YXTCHD:100, YZHCD:1, ZACJDBSD:90, TMPD:G1, PMGX:C4],42), ([DBYSLTD:1, DXPD:G1, GGBZL:1, GGSY:1, PSTJ:1, YXTCHD:100, YZHCD:1, ZACJDBSD:90, TMPD:G1, BCTRZD:QR, PMGX:C4],40)";
        //  InputStream inputStream = new ByteArrayInputStream(str.getBytes());

        int  rowCount=0;

        if(is.toString().length()>0){

            rowCount =iDataHandleService.InsertFPGData(is);
        }
        if(rowCount > 0){
            return ServerResponse.createBySuccess(rowCount);
        }
        else {
            return ServerResponse.createByErrorMessage("no");

        }*/
        ConnectToLinux ct = new ConnectToLinux(PropertiesUtil.getProperty("hadoop.server.master.ip"), PropertiesUtil.getProperty("hadoop.server.master.user"), PropertiesUtil.getProperty("hadoop.server.master.password"));
        //Const test = new Const();
        String result = ct.execute("cat " + String.format(PropertiesUtil.getProperty("hadoop.server.command.fpg.outputpath"), guid));
        if (result.length() > 0 && !result.toLowerCase().contains("no such file or directory")) {
            int rowCount = iDataHandleService.InsertFPGData(result, guid);
            ct.executeShell(String.format(PropertiesUtil.getProperty("hadoop.server.command.fgp.delete"), guid, guid, guid));
            if (rowCount > 0) {
                return ServerResponse.createBySuccess(rowCount);
            } else
                return ServerResponse.createByErrorMessage("数据存储错误");
        } else {
            return ServerResponse.createByErrorMessage("no");
        }
    }

    @ResponseBody
    @RequestMapping("/getYS1.do")
    public ServerResponse<List<String>> getPJYS1(String guid) {

        return iDataHandleService.getYs1List(guid);
    }

    @ResponseBody
    @RequestMapping("/getYS2.do")
    public ServerResponse<List<String>> getPJYS2(String YS1, String guid) {
        return iDataHandleService.getYs2List(YS1, guid);
    }

    @ResponseBody
    @RequestMapping("/getYsglTable.do")
    public ServerResponse<String[][]> getYsglTable(String YS1, String YS2, String guid) {
        return iDataHandleService.getYsData(YS1, YS2, guid);

    }

    @ResponseBody
    @RequestMapping("getFpgRule.do")
    public ServerResponse getFpgRule(double confidencePro, String year, String xzqdm) {
        ConnectToLinux ct = new ConnectToLinux(PropertiesUtil.getProperty("hadoop.server.master.ip"), PropertiesUtil.getProperty("hadoop.server.master.user"), PropertiesUtil.getProperty("hadoop.server.master.password"));
        String result = ct.execute("cat " + String.format(PropertiesUtil.getProperty("hadoop.local.command.fpg.outputpath"), year + xzqdm));
        //    String fList = ct.execute("cat " + String.format(PropertiesUtil.getProperty("hadoop.local.command.fList.outputpath"), year + xzqdm));

        if (result.length() > 0 && !result.toLowerCase().contains("no such file or directory")) {
            //    String pathRoot = request.getSession().getServletContext().getRealPath("") + "\\upFileDir\\" + year + File.separator + xzqdm;

            Map<String, List<String>> list = iDataHandleService.getFpgRule(confidencePro, result, year, xzqdm);

            if (list != null) {
                return ServerResponse.createBySuccess("置信度：" + confidencePro, list);
            } else
                return ServerResponse.createByErrorMessage("获取规则失败");
        } else {
            return ServerResponse.createByErrorMessage("算法结果文件不存在");
        }
    }

    //endregion

    //region RandomForest
    @ResponseBody
    @RequestMapping(value = "checkRandomForest.do")
    public ServerResponse checkRandomForestData(String guid) throws IOException {
        ConnectToLinux ct = new ConnectToLinux(PropertiesUtil.getProperty("hadoop.server.master.ip"), PropertiesUtil.getProperty("hadoop.server.master.user"), PropertiesUtil.getProperty("hadoop.server.master.password"));

        String result = ct.execute(String.format(PropertiesUtil.getProperty("hadoop.server.command.RandomForest.endPath"), guid));
        if (result.length() > 0 && !result.toLowerCase().contains("no such file or directory")) {
            result = ct.execute(String.format(PropertiesUtil.getProperty("hadoop.server.command.RandomForest.outputpath"), guid), "utf-8");
            int rowCount = iDataHandleService.InsertRandomForestData(result, guid);
            if (rowCount > 0) {
                return ServerResponse.createBySuccess(rowCount);
            } else {
                return ServerResponse.createByErrorMessage("数据存储错误");
            }

        } else {
            return ServerResponse.createByErrorMessage("no");
        }

    }

    @ResponseBody
    @RequestMapping(value = "/getRFData.do")
    public ServerResponse<ForestRandom> getRFData(String guid) {
        return iDataHandleService.getRFData(guid);

    }

    //endregion

    //region HLXFX
    @ResponseBody
    @RequestMapping(value = "checkHLXFX.do")
    public ServerResponse checkHlxfxData(String guid) {
        ConnectToLinux ct = new ConnectToLinux(PropertiesUtil.getProperty("hadoop.server.master.ip"), PropertiesUtil.getProperty("hadoop.server.master.user"), PropertiesUtil.getProperty("hadoop.server.master.password"));
        String result = ct.execute(String.format(PropertiesUtil.getProperty("hadoop.server.command.HLXFX.endPath"), guid));//hadoop hadoop fs -ls /predictions，1.直接在HDFS上判断是否存在，2.下载下来，判断是否存在out文件
        if (result.length() > 0 && !result.toLowerCase().contains("no such file or directory")) {
            result = ct.execute(String.format(PropertiesUtil.getProperty("hadoop.server.command.HLXFX.outputpath"), guid), "UTF-8");//相当于矩阵文件
            int rowCount = 0;
            rowCount = iDataHandleService.InsertHlxfxData(result, guid);
            if (rowCount > 0) {
                return ServerResponse.createBySuccess(rowCount);
            } else {
                return ServerResponse.createByErrorMessage("no");
            }
        } else {
            return ServerResponse.createByErrorMessage("no");
        }
    }

    @ResponseBody
    @RequestMapping(value = "getHlxfxYS.do")
    public ServerResponse checkHlxfxYS(String guid) {
        return iDataHandleService.getHlxfxYsList(guid);
    }

    @ResponseBody
    @RequestMapping(value = "getHlxfxTable.do")
    public ServerResponse<String[][]> checkHlxfxTable(String YS_name, String guid) {
        return iDataHandleService.getHlxfxResult(YS_name, guid);
    }
    //endregion

    //region 数据展示界面

    /**
     * 获取行政区树状结构树
     * 请求方法：GET
     *
     * @return 成功：{List<Object>树状结构}  失败：{status: 1, msg:"获取行政区失败！"}
     */
    @ResponseBody
    @RequestMapping(value = "getXzq.do", method = RequestMethod.GET)
    public ServerResponse<List<Object>> getXzq() {
        return iDataHandleService.getAllXqz();
    }

    /**
     * 地图文件树状结构
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getGeodata.do", method = RequestMethod.GET)
    public ServerResponse<Map<String, List<Object>>> getGeodata() {
        return iDataHandleService.getGeodata();
    }

    @ResponseBody
    @RequestMapping(value = "getGeodateByXzqdm.do", method = RequestMethod.GET)
    public ServerResponse<Map<String, List<Object>>> getGeodataByXzqdm(String xzqdm){
        return iDataHandleService.getGeodataByXzqdm(xzqdm);
    }

    //endregion

}
